<?php
// 公共函数
function C($param=""){
	$C = include_once(__ROOT__.'config.php');
	if( empty($param) ){
		return $C;
	}
	if( isset($C[$param]) ){
		return $C[$param];
	}
	return false;
}

function G($param){
	$val = '';
	if( isset($_POST[$param]) ){
		$val = $_POST[$param];
	} else if( isset($_GET[$param]) ) {
		$val = $_GET[$param];
	}
	if( !get_magic_quotes_gpc() ){
		$val = addslashes($val);
	}
	return trim($val);
}

function get_data($table=""){
	$data = array();
	if( empty($table) ){
		$arrPost = array_keys($_POST);
		foreach( $arrPost as $v ){
			$data[$v] = G($v);	
		}
	}else{
		if( file_exists(__TABLE__.$table.'.php') ){
			$table = include_once(__TABLE__.$table.'.php');
			if( is_array($table) ){
				foreach( $table as $k=>$v ){
					$data[$k] = G($v);
				}
			}
		} else {
			$model = new Model($table);
			$fields = $model->get_columns();
			foreach( $fields as $v ){
				$data[$v] = G($v);
			}
		}	
	}	
	return $data;
}


function ajax_return($array){
	echo json_encode($array);
	exit;
}


function load_util($className){
	if( file_exists(__LIB__.'util/'.$className.'.class.php') ){
		require_once(__LIB__.'util/'.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function load_model($className){
	$className = $className.'Model';
	if( file_exists(__MODEL__.$className.'.class.php') ){
		require_once(__MODEL__.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function load_controller($controller){
	if( !empty($controller) ){
		$arr = explode('.',$controller);
	}else{
		return false;
	}
	$className = $arr[count($arr)-1].'Controller';
	unset($arr[count($arr)-1]);
	$path = "";
	if( is_array($arr) && count($arr)>0 ){
		foreach($arr as $v){
			$path .= $v.'/';
		}
	}
	if( file_exists(__CONTROLLER__.$path.$className.'.class.php') ){
		require_once(__CONTROLLER__.$path.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function build_tree( $cates, &$parent, &$return ){
	if( !is_array($cates) ) return false;
	foreach( $cates as $k=>$v ){
		$return[] = $v;
		if( isset($parent[$v['id']]) ){
			build_tree( $parent[$v['id']],$parent, $return );
		}
		unset($parent[$v['id']]);
	}
}

function tree($arr,$sep='&nbsp;&nbsp;'){
	$arr_parents = array();
	$new_arr = array();
	foreach( $arr as $v ){
		$tmp = array(
			'id'	=> $v['id'],
			'name'	=> $v['name'],
			'pid'	=> $v['pid']
			); 
		if( $v['pid']==0 ) $tmp['level'] = 0;
		$arr_parents[$v['pid']][] = $tmp;
		$new_arr[$v['id']] = $tmp;
	}	
	ksort($arr_parents);
	foreach( $arr_parents as $key=>$val ){
		foreach( $val as $k=>$v ){
			$pid = $arr_parents[$key][$k]['pid'];
			$level = isset($new_arr[$pid]['level']) ? $new_arr[$pid]['level']+1 : 0;
			$new_arr[$v['id']]['level'] = $level;
			$arr_parents[$key][$k]['level'] = $level;
		}
	}
	$tmp = array_slice($arr_parents,0,1);
	$return = array();
	build_tree( $tmp[0], $arr_parents, $return);
	foreach( $return as $k=>$v ){
		$multiplier = $v['level'] * 2;
		$return[$k]['name'] = str_repeat($sep,$multiplier).$v['name'];
	}
	return $return;
}

function alert( $msg,$url="" ){
	if( $url==-1 ){
		$url = "";
	}elseif( !empty($url) ){
		$url = 'location.href="'.$url.'";';
	}else{
		$url = 'history.back();';
	}
	echo '<script type="text/javascript">alert("'.$msg.'");'.$url.'</script>';
}

function tips( $msg, $type=1, $reload=0, $redirect="" ){
	echo '<script type="text/javascript">tips("'.$msg.'",'.$type.','.$reload.',"'.$redirect.'");</script>';
}

function create_folder($path, $mode=0777) { 
	if (!file_exists($path)) { 
		create_folder(dirname($path)); 
		@mkdir($path, $mode); 
	} 
} 

function substring($str, $start, $len=""){
	if( function_exists('mb_substr') ){
		return mb_substr($str, $start, $len);
	}else{
		return substr($str, $start, $len);
	}
}
